package javathread;

public class BankTest {

}

/**
 * 懒汉式单例模式
 */
class Bank{
    private Bank(){};
    private  static Bank instance=null;
    public Bank getInstance(){
        //方法一效率差，因为每一个线程到这里都要加锁
//        synchronized (Bank.class) {
//            if (instance == null) {
//                instance = new Bank();
//            }
//        }
        //改进，在方法一的基础上加一个判断，instance == null就进去加锁，instance ！= null就直接出去，这样就会大大提升效率
        if (instance == null){
            synchronized (Bank.class) {
               if (instance == null) {
                   instance = new Bank();
               }
            }
        }
        return  instance;
    }
}
